/*
 * OpenParts
 * A dynamic-loading components framework for GWT
 * Copyright (C) 2011 Christophe Bouthier  [chris{AT}binary-gastronome{DOT}fr]
 *
 * This work is partially based on work I have done at INRIA (http://www.inria.fr) 
 * in the context of the Qualipso European Project (http://qualipso.org/),
 * The work done at INRIA is Copyright (C) 2006-2011 INRIA
 *
 * This work is distributed under the LGPL version 3
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation. See the GNU
 * Lesser General Public License in LGPL.txt for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package fr.openparts.OpenParts.test.gwt.tests;

import com.google.gwt.junit.client.GWTTestCase;

import fr.openparts.OpenParts.client.OPCommand;
import fr.openparts.OpenParts.client.OPCommandFactory;
import fr.openparts.OpenParts.client.OPSchedulerBinding;

/**
 * GWT tests for OPSchedulerBinding.
 * 
 * @author Christophe Bouthier [chris{AT}binary-gastronome{DOT}fr]
 * @creation.date 09 November 2010
 */
public class OPSchedulerBindingTestGwt extends GWTTestCase {

    private OPSchedulerBinding binding;
    private OPCommand command;
    private OPSchedulerMocked scheduler;

    /**
     * Return the name of the tested GWT Module.
     * 
     * @see com.google.gwt.junit.client.GWTTestCase#getModuleName()
     */
    @Override
    public final String getModuleName() {
        return "fr.openparts.OpenParts.test.gwt.TestOpenParts";
    }

    /**
     * Called at the start of each test to set up the action.
     * 
     * @see com.google.gwt.junit.client.GWTTestCase#gwtSetUp()
     */
    @Override
    protected void gwtSetUp() throws Exception {
        super.gwtSetUp();
        binding = OPSchedulerBinding.schedulerBindingFactory();
        command = OPCommand.commandFactory(OPCommandFactory.LOAD_PART, null, null, null);
        scheduler = new OPSchedulerMocked();
    }

    /**
     * Test creation of scheduler binding. Should get something not null.
     */
    public final void testCreatingSchedulerBinding() {
        assertNotNull("scheduler binding shouldn't be null", binding);
    }

    /**
     * Test scheduling a null command. Should raise an assertion error.
     */
    public final void testSchedulingCommandWithNullCommand() {
        if (assertionsAreEnabled()) {
            try {
                binding.scheduleCommand(null);
                fail("OPSchedulerBinding should raise an assertion error when trying to schedule a null command.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test scheduling a command when the scheduler is not binded. Should NOT raise an error.
     */
    public final void testSchedulingCommandWithoutScheduler() {
        try {
            binding.scheduleCommand(command);
        } catch (Throwable e) {
            fail("OPSchedulerBinding should NOT raise an error when scheduling a command without scheduler first binded.");
        }
    }

    /**
     * Test scheduling a command. Should call the (mocked) scheduler.
     */
    public final void testSchedulingCommand() {
        registerScheduler(binding, scheduler);
        assertEquals("scheduleCommand should not have been called yet", 0, scheduler.scheduleCommandCalled);
        binding.scheduleCommand(command);
        assertEquals("scheduleCommand should have been called now", 1, scheduler.scheduleCommandCalled);
    }

    /**
     * Mock class for scheduler.
     */
    final class OPSchedulerMocked {
        public int scheduleCommandCalled = 0;
    }

    /**
     * Register this class as a mock OPScheduler in the given binding.
     * 
     * @param binding
     *            the binding on which to register
     */
    private final native void registerScheduler(final OPSchedulerBinding binding, final OPSchedulerMocked scheduler) /*-{
        binding.scheduleCommand = function(request) {
            ++scheduler.@fr.openparts.OpenParts.test.gwt.tests.OPSchedulerBindingTestGwt.OPSchedulerMocked::scheduleCommandCalled;
        };
    }-*/;

    /**
     * Check if assertions are enabled.
     * 
     * @return true if assertions are enabled, false otherwise
     */
    private final boolean assertionsAreEnabled() {
        try {
            assert false;
            return false;
        } catch (AssertionError e) {
            return true;
        }
    }
    
}
